package edu.tsinghua.lumaqq.qq.packets.in._08;

import java.nio.ByteBuffer;

import edu.tsinghua.lumaqq.qq._08QQ;
import edu.tsinghua.lumaqq.qq.beans.QQUser;
import edu.tsinghua.lumaqq.qq.packets.PacketParseException;
import edu.tsinghua.lumaqq.qq.packets._08BasicInPacket;

/**
 * 1. 包头 1字节 0x02
 * 2. QQ版本 2字节 0x113f
 * 3. 命令字 2字节 0x00ba
 * 4. 包序号 2字节 0x2679
 * 5. 加密包体 48字节
 * 6. 包尾 1字节 0x03
 *
 * @author luna
 */
public class _08LoginToken2ReplyPacket extends _08BasicInPacket {

    public byte[] loginToken2;
    public byte subCommand;
    public byte replyCode;
    public byte[] puzzleData;
    public byte[] nextImageToken;
    public int fragmentIndex;
    public int nextFragmentIndex;

    public _08LoginToken2ReplyPacket(ByteBuffer buf, int length, QQUser user)
            throws PacketParseException {
        super(buf, length, user);
    }

    /*
      * (non-Javadoc)
      *
      * @see edu.tsinghua.lumaqq.qq.packets._08InPacket#getPacketName()
      */
    @Override
    public String getPacketName() {
        return "Login Token 2 Reply _08Packet";
    }

    @Override
    public byte[] getDecryptKey(byte[] body) {
        return user.getPasswordKey();
    }

    @Override
    public byte[] getFallbackDecryptKey(byte[] body) {
        return user.getLoginTokenRandomKey();
    }

    @Override
    protected void parseBody(ByteBuffer buf) throws PacketParseException {
        subCommand = buf.get();
        buf.getChar();
        replyCode = buf.get();
        char len = buf.getChar();
        loginToken2 = new byte[len];
        buf.get(loginToken2);
        switch (replyCode) {
            case _08QQ.QQ_REPLY_LOGIN_NEED_VERIFY:
                len = buf.getChar();
                puzzleData = new byte[len];
                buf.get(puzzleData);
                //下面俩字节
                fragmentIndex = (buf.get() & ((byte) 0xFF));
                nextFragmentIndex = (buf.get() & ((byte) 0xFF));

                len = buf.getChar();
                nextImageToken = new byte[len];
                buf.get(nextImageToken);
                break;
        }
    }

}
